package com.example.hidesms.receiver;

import java.lang.reflect.Method;

import android.content.Context;
import android.media.AudioManager;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;

import com.android.internal.telephony.ITelephony;

public class MyPhoneStateListener extends PhoneStateListener {

	private boolean idle;
	private boolean offhook;
	private boolean ringing;
	private Context context;
	private static String number;

	public boolean isIdle() {
		return idle;
	}

	public void setIdle(boolean idle) {
		this.idle = idle;
	}

	public boolean isOffhook() {
		return offhook;
	}

	public void setOffhook(boolean offhook) {
		this.offhook = offhook;
	}

	public boolean isRinging() {
		return ringing;
	}

	public void setRinging(boolean ringing) {
		this.ringing = ringing;
	}

	public MyPhoneStateListener(Context context) {
		this.context = context;
	}

	public MyPhoneStateListener(boolean idle, boolean offhook, boolean ringing) {
		this.idle = idle;
		this.offhook = offhook;
		this.ringing = ringing;
	}

	public void onCallStateChanged(int state, String incomingNumber) {
		switch (state) {
		case TelephonyManager.CALL_STATE_RINGING:
			Log.e("oncall", "ok");
			AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
            //Turn ON the mute
            audioManager.setStreamMute(AudioManager.STREAM_RING, true);
            //i want to getnum from here
            //end of getting num
            TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            try {
                Class clazz = Class.forName(telephonyManager.getClass().getName());
                Method method = clazz.getDeclaredMethod("getITelephony");
                method.setAccessible(true);
                ITelephony telephonyService = (ITelephony) method.invoke(telephonyManager);
                //Checking incoming call number
                if (incomingNumber.contains("982614213")) {
                    telephonyService.endCall();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            //Turn OFF the mute
            audioManager.setStreamMute(AudioManager.STREAM_RING, false);
            break;
//			number = incomingNumber;
//			setRinging(true);
//			break;
		case TelephonyManager.CALL_STATE_OFFHOOK:
			setOffhook(true);
			break;
		case TelephonyManager.CALL_STATE_IDLE:
			setIDELTrue();

			setServiceEnabled(context, isIdle());
			break;
		}
	}

	public void setIDELTrue() {
		if (isRinging() || isOffhook()) {
			setIdle(true);
		} else {
			setIdle(false);
		}
	}

	public static void setServiceEnabled(Context context, boolean enabled) {
//		MyPhoneStateService.mEnable = enabled;
//		Intent it = new Intent(context, MyPhoneStateService.class);
//		if (enabled) {
//			it.putExtra("number", number);
//			context.startService(it);
//			// Log.i("22222", "MyPhoneStateLis");
//		}
//
//		else {
//			context.stopService(it);
//		}
	}

}